package com.reyco1.media.audio
{	
	import com.reyco1.util.ArrayUtil;

	public class CuePointManager
	{
		public  var accuracy:Number = 100;
		private var cuePoints:Vector.<AudioCuePoint>;
		
		public function CuePointManager( acc:int = 100 )
		{
			cuePoints = new Vector.<AudioCuePoint>();
			accuracy = acc;
		}
		
		public function add(time:Number, method:Function, params:Array = null):void
		{
			cuePoints.push( new AudioCuePoint(time, method, params) );
		}
		
		public function clear():void
		{
			while( cuePoints.length > 0 )
			{
				cuePoints[0].clear();
				cuePoints.splice(0, 1);
			}
			cuePoints = null;
		}
		
		public function update( progress:int ):void
		{
			for (var a:int = 0; a < cuePoints.length; a++) 
			{
				if(!cuePoints[a].used)
				{
					if( progress >= (cuePoints[a].time - accuracy) && progress <= (cuePoints[a].time + accuracy) )
					{
						cuePoints[a].execute();
					}
				}
			}			
		}
		
		public function remove(time:Number):void
		{
			for (var a:int = 0; a < cuePoints.length; a++) 
			{
				if(cuePoints[a].time == time)
				{
					cuePoints.splice(a, 1);
					break;
				}
			}
		}
	}
}